//create by: 邓志锋 <280160522@qq.com> <http://www.diygw.com>
var util = require('../../../utils/util.js')
const App = getApp();
const navigationBarHeight = (getApp().diygwstatusBarHeight + 44) + 'px'
Page({
  data: {
    checkboxItems1: [{
        "checked": false,
        "label": "学习计划",
        "value": "学习计划"
      },
      {
        "checked": false,
        "label": "生活计划",
        "value": "生活计划"
      },
      {
        "checked": false,
        "label": "其他计划",
        "value": "其他计划"
      }
    ],
    date2: '1900-01-01',
    date3: '1900-01-01',
    swicthclz4: 'weui-switch-checked',
    switchchecked4: true,
    buttons5: [{
      type: 'button',
      url: '',
      title: ''
    }],
    planName: '',
    type: '',
    beginTime: '',
    endTime: '',
    status: '',
    userId: '',
    //addTime;
  },

  onShareAppMessage: function() {

  },

  getCurrentDate: function() {
    const date = new Date();
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    return [year, month, day].join('-')
  },
  onLoad(option) {
    if (option) {
      this.setData({
        globalOption: option
      })
    }
    var time = this.getCurrentDate();
    this.setData({
      beginTime: time,
      endTime: time
    });
    

  },
  // onLoad: function () {
  //   var time = getCurrentDate();
  //   this.setData({
  //     beginTime: time,
  //     endTime: time
  //   });
  // },
  onShow() {
    
  },
  // 计划类型
  changeplan_type: function(values) {
    if (values.detail) {
      values = values.detail.value
    }
    var checkboxItems1 = this.data.checkboxItems1;
    for (var i = 0, lenI = checkboxItems1.length; i < lenI; ++i) {
      checkboxItems1[i].checked = false;
      for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (checkboxItems1[i].value == values[j]) {
          checkboxItems1[i].checked = true;
          break;
        }
      }
    }
    this.setData({
      checkboxItems1: checkboxItems1,
      type: values
    });
  },
  // 开始时间
  bindDateChange: function(e) {
    this.setData({
      beginTime: e.detail.value
    });
  },
  // 结束时间
  bindDateChange2: function(e) {
    this.setData({
      endTime: e.detail.value
    });
  },
  changeplan_name: function(e) {
    this.setData({
      planName: e.detail.value,
    });
  },
  switch1Change: function(e) {
    console.log('switch1 发生 change 事件，携带值为', e.detail.value);
    this.setData({
      status: e.detail.value
    });
  },
  submit() {
    let planName = this.data.planName;
    let type = this.data.type;
    let beginTime = this.data.beginTime;
    let endTime = this.data.endTime;
    let status = this.data.status;
    let userId = App.globalData.userId;
    console.log(JSON.stringify(this.data));
    //校验用户名
    if (planName.length < 1) {
      wx.showToast({
        icon: 'none',
        title: '请输入计划名称',
      })
      return
    }
  
    //校验账号
    if (type.length < 1) {
      wx.showToast({
        icon: 'none',
        title: '请选择类型',
      })
      return
    }
    // //校验密码
    // if (mima.length < 4) {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '密码至少4位',
    //   })
    //   return
    // }


    let data ={
       planName : planName,
      type: type.join("、"),
       beginTime : beginTime,
       endTime : endTime,
       status : status,
       userId : App.globalData.userId,
    }
    var requestUrl = App.globalData.apiBase + "/api/plan/add";
    //this.data.requestUrl = requestUrl;
    util.ajax(requestUrl, "post", data, this.processData)

  },
  processData: function (data) {
   
    console.log(data);
    if (data.code == 200){
      // wx.navigateTo({
      //   url: "/pages/plan/plan"
      // })
      var pages = getCurrentPages();
      if (pages.length > 1) {
        //上一个页面实例对象
        var prePage = pages[pages.length - 2];
        //关键在这里
        //prePage.onLoad();


        // wx.navigateTo({       //非tabBar页面的跳转
        //   url: '/' + prePage.route,
        //   success: function () {
        //     if (prePage.route == 'pages/plan/plan') {
        //       prePage.syncPageData()
        //     }
        //   },
        //   fail: (err) => {
        //     console.log(err)
        //   }
        // })
    wx.navigateBack({
      delta: 1, //返回的页面数，如果 delta 大于现有页面数，则返回到首页。
      success: function () {
        if (prePage.route == 'pages/plan/plan') {
          prePage.onLoad() //这个函数式调用接口的函数
        }
      }
    })
      } 


      
    }
   
  },
})